package com.lge.clock.worldclock.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import com.lge.clock.R;
import com.lge.clock.worldclock.util.DataManager.BinaryDataFile;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Rect;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author lge.com
 */
public class DataManager {
	private static final String DATA_BINARY_FILE_NAME_NORTH = "data_earth_north.dat";
	private static final String DATA_BINARY_FILE_NAME_SOUTH = "data_earth_south.dat";

	private static final String north_hemisphere = "north_hemisphere";
	private static final String south_hemisphere = "south_hemisphere";

	private static int minimum_earth_north_image_resource_id = R.drawable.minimum_earth_north;
	private static int minimum_earth_south_image_resource_id = R.drawable.minimum_earth_south;

	private static int earth_north_image_resource_id = R.drawable.earth_north;
	private static int earth_south_image_resource_id = R.drawable.earth_south;

	public static final int[] earth_level0_image_resource_id_list = {
		minimum_earth_north_image_resource_id,
		minimum_earth_south_image_resource_id
	};

	public static final int[] earth_level1_image_resource_id_list = {
		earth_north_image_resource_id,
		earth_south_image_resource_id
	};

	public static final String[] earth_gl_object_name_list = {
		north_hemisphere,
		south_hemisphere
	};

	public static final String[] earth_gl_data_file_list = {
		DATA_BINARY_FILE_NAME_NORTH,
		DATA_BINARY_FILE_NAME_SOUTH
	};

	public static final float PI = (float) Math.PI;
	public static final float TWO_PI = (float) (2.0 * PI);
	public static final float DEGREES_TO_RADIANS = (float) Math.PI / 180.0f;

	public static final int LOCALE_ENGLISH = 0;
	public static final int LOCALE_KOREAN = 1;

	private static final float HVGA_SHAPE_LOCATION_Y_COORDINATE_LANDSCAPE = -0.615f;
	private static final float WVGA_SHAPE_LOCATION_Y_COORDINATE_LANDSCAPE = -0.75f;
	
	private static int lcd_width = 0; 
	private static int lcd_height = 0; 
	private static int status_bar_height = 0; 
	private static int title_height = 0; 
	
	private static ArrayList<GLDataObj> glObjList = new ArrayList<GLDataObj>();

	private static float cityVertices[] = null;

	public static GLDataObj getGlObj(String name) {
	    for (int i = 0 ; i <glObjList.size() ; i++){
	        if (glObjList.get(i).getObjName().equals(name)){
	            return glObjList.get(i);
	        }
	    }
	    return null;
	}

	public static float[] getCityVertices() {
		return cityVertices;
	}

	public static void loadLCDSize(Context context) {
		  Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		  int width = display.getWidth();
		  int height = display.getHeight();
		  lcd_height = Math.max(width, height);
		  lcd_width = Math.min(width, height); 
	}
	
	public static void loadDisplayFrameInfo(Window window) {
		Rect rectgle= new Rect();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
		status_bar_height = rectgle.top;
		title_height = contentViewTop - status_bar_height;
	}
	
	public static int getSurfaceViewHeight() {
		return (lcd_height - status_bar_height - title_height);
	}
	
	/** Add to Shape Translate Y value for Supported LCD SIZE */
	public static float getShapeTranslateY() {
		float returnValue = 0.0f;
		if(lcd_width == 480 && lcd_height == 800) {
			returnValue = WVGA_SHAPE_LOCATION_Y_COORDINATE_LANDSCAPE;
		}
		else if(lcd_width == 320 && lcd_height == 480) {
			returnValue = HVGA_SHAPE_LOCATION_Y_COORDINATE_LANDSCAPE;
		}
		return returnValue;
	}
	
	public static void loadAssetsBinaryFile(Context context) throws StreamCorruptedException, IOException {
		for(int i = 0; i < earth_gl_data_file_list.length; i++) {
			loadAssetsBinaryFile(context, earth_gl_data_file_list[i], earth_gl_object_name_list[i]);
		}
	}

	public static void loadAssetsBinaryFile(Context context, String fileName, String glObjName) throws StreamCorruptedException, IOException {
		AssetManager assetManager = context.getAssets();
		
		GLDataObj glObj = new GLDataObj();
		glObj.setObjName(glObjName);
		
		ObjectInputStream objectInputStream = new ObjectInputStream(assetManager.open(fileName));
		
		try {
			BinaryDataFile dataFile = (BinaryDataFile)objectInputStream.readObject();
			if(dataFile != null) {
				glObj.setVertices(dataFile.mVertexData);
				glObj.setTextures(dataFile.mTextureData);
				glObjList.add(glObj);
			}
		} catch (Exception e) {
			android.util.Log.e("ERROR",  e.getMessage());
			e.printStackTrace();
		} finally {
			if (objectInputStream != null) objectInputStream.close();
		}
	}

	public static float[] convertToCoordinate (double degLatitude, double degLongitude, float scaleSize){
        float vertex[] = new float[3];
        
        double latitude = degToRad(degLatitude);
        double longitude = degToRad(degLongitude);
        double z = scaleSize*Math.sin(latitude);
        double x = scaleSize*Math.cos(latitude)*Math.sin(longitude);
        double y = scaleSize*Math.cos(latitude)*Math.cos(longitude);
        vertex[0]=Double.valueOf(x).floatValue();
        vertex[1]=Double.valueOf(y).floatValue();
        vertex[2]=Double.valueOf(z).floatValue();
        return vertex;
    }

    public static double degToRad (double deg){
        return deg*(Math.PI/180);
    }

    public static String strToPolar(String data){
        String rString = data;
        String temp[] = data.split("\\.");
        if(temp.length > 1) {
        	temp[1] = String.valueOf(Integer.parseInt(temp[1])*100/60);
        	rString = temp[0] + "." + temp[1];
        }
        return rString;
    }

    public static int getTimeAsInt(String strTime)
    {
        int intTime = 0;
            
        String[] arrTime = strTime.split(":");
        int nHour   = Integer.parseInt(arrTime[0]);
        int nMinute = Integer.parseInt(arrTime[1].substring(0, 2));
        
        if( nHour >= 0 ) {
            intTime = nHour * 60 + nMinute;
        }
        else {
            intTime = nHour * 60 + nMinute;
        }
      
        return intTime;
    }
    
    public static String getGMTAsString(int intTime) {
    	String retStr = "GMT ";
    	int intMinute = 0;
    	
    	intTime /= 60000;
    	
    	if(intTime > 0) retStr += "+ ";
    	else retStr += "- ";

    	retStr += String.format("%d", Math.abs(intTime/60));
    	
    	intMinute = intTime%60;
    	if(intMinute != 0) {
    		retStr = retStr + ":" + String.format("%02d",Math.abs(intMinute));
    	}
    	
    	return retStr;
    }

    /**
     * @author lge.com
     */
    static class BinaryDataFile implements Serializable {
    	private static final long serialVersionUID =8123527480892830208L;
        public float[] mVertexData;
        public float[] mTextureData;
        
        BinaryDataFile(float[] vertex, float[] texture) {
            mVertexData = vertex;
            mTextureData = texture;
        }
    }
}
